package com.caac.core.web.action.setting;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.caac.core.dm.User;
import com.caac.core.dm.ViewProp;
import com.caac.core.service.ViewPropManager;
import com.caac.core.util.HttpUtils;
import com.caac.core.web.action.BaseAction;
import com.caac.core.web.form.BaseForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright Intolong 2004</p>
 * <p>Company: Intolong</p>
 * @author <a href="mailto:intolong@163.com">Intolong</a>
 * @version 1.0
 */

public class DoSaveViewPropAction extends BaseAction {
    private static final String KEY_GROUP_NAME = "groupName";
    private static final String KEY_PROP_NAMES = "propNames";

    protected ActionForward doExecute(ActionMapping mapping, BaseForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response) throws
            Exception {
        User user = this.getUserFromCache(request, response);
        String username = null;
        if (user != null) {
            username = user.getUsername();
        }
        String groupName = HttpUtils.getParameter(request, KEY_GROUP_NAME);
        String[] propNames = HttpUtils.getParameters(request, KEY_PROP_NAMES);
        ViewPropManager manager = getViewPropManager();
        manager.saveViewProps(groupName, propNames, username);
        List selProps = manager.getSelectedViewPropsByGroupName(groupName,
                username);
        String propLabels = "";
        if (propNames != null) {
            for (int i = 0; i < selProps.size(); i++) {
                propLabels += ((ViewProp) selProps.get(i)).getLabel() + "\n";
            }
        }
        request.setAttribute("propLabels", propLabels);
        return forward(mapping);
    }
}
